package com.sky.service.admin.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.admin.OrdersMapper;
import com.sky.mapper.user.DishMapper;
import com.sky.mapper.user.SetmealMapper;
import com.sky.mapper.user.UserMapper;
import com.sky.service.admin.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;

import java.util.Map;


@Service("adminWorkSpaceService")
public class WorkSpaceServiceImpl implements WorkSpaceService {



    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO businessData(LocalDate begin, LocalDate end) {

        // 获取日期时间
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

        HashMap map = new HashMap<>();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        // 新增用户数
        Integer newUsers = userMapper.newUsers(map);

        map.put("status", Orders.COMPLETED);
        // 有效订单数
        Integer validOrderCount = ordersMapper.orderCount(map);
        Integer allOrderCount = ordersMapper.orderCount(map);
        // 订单完成率
        if (allOrderCount == null ){
            allOrderCount = 0;
        }
        Double orderCompletionRate;
         orderCompletionRate = validOrderCount * 1.0 / allOrderCount;
         if (allOrderCount == 0){
             orderCompletionRate = 0.0;
         }


        // 营业额

        Double turnover;
        turnover = ordersMapper.orderAmountSum(map);
         if (turnover == null){
             turnover = 0.0;
         }




        // 平均客单价
        Double unitPrice;

        unitPrice = turnover * 1.0 / validOrderCount;
        if (validOrderCount == 0){
            unitPrice = 0.0;
        }







        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();

    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {

        Map<String, Integer> statusMap = new HashMap<>();
        statusMap.put("status", StatusConstant.ENABLE);
        // 查询已经起售套餐数量
        Integer discontinued = setmealMapper.overviewSetmeals(statusMap);

        // 查询已经停售套餐数量
        statusMap.replace("status", StatusConstant.DISABLE);
        Integer sold = setmealMapper.overviewSetmeals(statusMap);
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {
        // 查询已起售菜品数量
        HashMap<String, Integer> statusMap = new HashMap<>();
        statusMap.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.overviewDishes(statusMap);

        // 查询已停售菜品数量
        statusMap.replace("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.overviewDishes(statusMap);
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        HashMap<String, Integer> statusMap = new HashMap<>();
        // 查询全部订单数量
        Integer allOrders = ordersMapper.orderCount(statusMap);

        // 查询已经取消数量
        statusMap.put("status", Orders.CANCELLED);
        Integer cancelledOrders = ordersMapper.orderCount(statusMap);
        // 查询已经完成数量
        statusMap.replace("status", Orders.COMPLETED);
        Integer completedOrders = ordersMapper.orderCount(statusMap);
        // 查询待派送数量
        statusMap.replace("status", Orders.CONFIRMED);
        Integer confirmedOrders = ordersMapper.orderCount(statusMap);
        // 查询待接单数量
        statusMap.replace("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = ordersMapper.orderCount(statusMap);
        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(confirmedOrders)
                .waitingOrders(waitingOrders)
                .build();
    }
}
